package com.huilian.tool.meituan.service.impl;

import com.huilian.tool.meituan.config.MiddlewareConfig;
import com.huilian.tool.meituan.enums.FunctionEnum;
import com.huilian.tool.meituan.service.MeiTuanApiAskService;
import com.huilian.tool.meituan.util.HttpUtils;
import com.huilian.tool.meituan.util.HttpsMain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class MeiTuanApiAskServiceImpl implements MeiTuanApiAskService {



    /**
     * 美团V2 获取团购信息
     *
     * @param storeNo       美团门店ID
     * @param agencyConfig  机构信息 isvOrgId,privateKey必须有
     * @return java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> dealQueryshopdeal(String storeNo,HashMap<String,String> agencyConfig) throws IOException{
        Map<String, Object> form = new HashMap<>();
        form.put("storeNo", storeNo);
        form.put("brand", "meituan");
        String param = HttpUtils.format(agencyConfig, form, FunctionEnum.IOTBOX_TUANGOU_DEAL_QUERYSHOPDEAL);
        String response = HttpsMain.httpReq(MiddlewareConfig.meiTuan, param);
        //延签并返回map值
        final Map<String, Object> result = HttpUtils.getResultData(response);
        return result;
    }

    /**
     * 美团V2 验券
     *
     * @param storeNo       美团门店ID
     * @param verifyCode    券码
     * @param outTradeNo    订单号
     * @param agencyConfig  机构信息 isvOrgId,privateKey必须有
     * @return java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> receiptConsume(String storeNo, String verifyCode,String outTradeNo, HashMap<String, String> agencyConfig) throws IOException {
        Map<String, Object> form = new HashMap<>();
        form.put("storeNo", storeNo);
        form.put("verifyCode", verifyCode);
        form.put("outTradeNo", outTradeNo);
        form.put("brand", "meituan");
        String param = HttpUtils.format(agencyConfig, form, FunctionEnum.IOTBOX_VERIFY_CODE);
        String response = HttpsMain.httpReq(MiddlewareConfig.meiTuan, param);
        //延签并返回map值
        final Map<String, Object> result = HttpUtils.getResultData(response);
        return result;
    }

    /**
     * 美团验券校验
     * @param storeNo          美团门店ID
     * @param verifyCode       券码
     * @param agencyConfig     机构信息 isvOrgId,privateKey必须有
     */
    @Override
    public Map<String, Object> receiptPrepare(String storeNo, String verifyCode, HashMap<String, String> agencyConfig) throws IOException {
        Map<String, Object> form = new HashMap<>();
        form.put("storeNo", storeNo);
        form.put("verifyCode", verifyCode);
        form.put("brand", "meituan");
        String param = HttpUtils.format(agencyConfig, form, FunctionEnum.IOTBOX_VERIFY_CODE_CHECK);
        String response = HttpsMain.httpReq(MiddlewareConfig.meiTuan, param);
        //延签并返回map值
        final Map<String, Object> result = HttpUtils.getResultData(response);
        return result;
    }

    /**
     * 美团V2 撤销验券
     *
     * @param storeNo       美团门店ID
     * @param verifyCode    券码
     * @param outTradeNo    订单号
     * @param agencyConfig  机构信息  isvOrgId,privateKey必须有
     * @return java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> receiptReverseconsume(String storeNo, String verifyCode, String outTradeNo, HashMap<String, String> agencyConfig) throws IOException {
        Map<String, Object> form = new HashMap<>();
        form.put("storeNo", storeNo);
        form.put("verifyCode", verifyCode);
        form.put("outTradeNo", outTradeNo);
        form.put("brand", "meituan");
        String param = HttpUtils.format(agencyConfig, form, FunctionEnum.IOTBOX_CANCEL_VERIFY_CODE);
        String response = HttpsMain.httpReq(MiddlewareConfig.meiTuan, param);
        //延签并返回map值
        final Map<String, Object> result = HttpUtils.getResultData(response);
        return result;
    }

    /**
     * 获取授权门店
     *
     * @param storeName     已授权的门店名称 不是必填
     * @param agencyConfig  机构信息  isvOrgId,privateKey必须有
     * @return java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> getAuthorizedStores(String storeName, HashMap<String, String> agencyConfig) throws IOException {
        Map<String, Object> form = new HashMap<>();
        form.put("storeName", storeName);
        form.put("brand", "meituan");
        String param = HttpUtils.format(agencyConfig, form, FunctionEnum.IOTBOX_QUERY_AUTHORIZE_STORE);
        String response = HttpsMain.httpReq(MiddlewareConfig.meiTuan, param);
        //延签并返回map值
        final Map<String, Object> result = HttpUtils.getResultData(response);
        return result;
    }
}
